﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@* Views/Chat/Index.cshtml *@
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui 优化聊天页面</title>
    <!-- 引入 Layui CSS -->
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <style>
        .chat-container {
            max-width: 800px;
            max-height: 1000px;
            margin: 20px auto;
            border: 1px solid #e6e6e6;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }

        .chat-header {
            padding: 15px;
            background-color: #009688;
            color: white;
            border-radius: 5px 5px 0 0;
            font-size: 18px;
        }

        .chat-history {
            height: 500px;
            overflow-y: auto;
            padding: 15px;
            background-color: #f8f8f8;
        }

        .message {
            margin-bottom: 15px;
            max-width: 70%;
            padding: 10px 15px;
            border-radius: 5px;
            position: relative;
            word-wrap: break-word;
        }

        .user-message {
            background-color: #e2f5e9;
            margin-left: auto;
            border-top-right-radius: 0;
        }

        .bot-message {
            background-color: white;
            margin-right: auto;
            border-top-left-radius: 0;
        }

        .input-area {
            display: flex;
            padding: 15px;
            background-color: #f2f2f2;
            border-radius: 0 0 5px 5px;
        }

        .input-area input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 3px;
            outline: none;
        }

        .input-area button {
            margin-left: 10px;
        }

        .message-time {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
            text-align: right;
        }

        .navbar {
            display: none;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <div class="chat-container">
        <div class="chat-header layui-bg-green">
            <i class="layui-icon layui-icon-dialogue"></i> 智能聊天助手
        </div>

        <div id="chatHistory" class="chat-history">
            <!-- 动态加载聊天记录 -->
            <div class="message bot-message">
                <div>您好！我是智能助手，有什么可以帮您的吗？</div>
                <div class="message-time">系统消息</div>
            </div>
        </div>

        <div class="input-area">
            <input type="text" id="userInput" class="layui-input" placeholder="输入您的问题..." autocomplete="off">
            <button class="layui-btn layui-btn-green" onclick="sendMessage()">
                <i class="layui-icon layui-icon-release"></i> 发送
            </button>
        </div>
    </div>
</div>

<!-- 引入 Layui JS -->
<script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>


    <script>
        const sessionId = generateSessionId(); // 生成唯一会话ID

        async function sendMessage() {
            console.log(111);
            const input = document.getElementById('userInput');
            const message = input.value.trim();

            if (!message) return;

            // 添加到聊天界面
            appendMessage(message, 'user');
            input.value = '';

            // 调用API
            try {
                $.ajax({
                    url: 'http://localhost:5077/api/chat/send',
                    type: 'POST',
                    contentType:"application/json",
                    data: JSON.stringify({
                        sessionId: sessionId,
                        message: message
                    }),
                    success: function (data) {
                        console.log(data);
                         appendMessage(data.reply, 'bot');
                    },
                })
            } catch (error) {
                console.log(123,error);
                appendMessage("客服系统暂时不可用", 'error');
            }
        }

        function appendMessage(text, sender) {
            const history = document.getElementById('chatHistory');
            const msgElement = document.createElement('div');
            msgElement.className = `message ${sender}`;
            msgElement.textContent = text;
            msgElement.style.whiteSpace = 'pre-wrap';
            history.appendChild(msgElement);
            history.scrollTop = history.scrollHeight;
        }

        function generateSessionId() {
            return 'session-' + Math.random().toString(36).substr(2, 9);
        }
    </script>

</body>
</html>
